# -*- coding:utf-8 -*-
from models import *
from django.contrib import admin
from django.http import HttpResponse

class EmployeeInline(admin.TabularInline):
    model = Employee
    extra = 0#定义显示条数

class EmployeeGroupAdmin(admin.ModelAdmin): 
    inlines = [EmployeeInline] 
    
class PhotosInline(admin.TabularInline):
    
    model = Photos
    extra = 0#定义显示条数

class PhotosSortAdmin(admin.ModelAdmin): 
    list_display = ('title',)

class BanjiAdmin(admin.ModelAdmin): 
    list_display = ('period','ban')
    
    
class GradeAdmin(admin.ModelAdmin): 
    fieldsets = [
        ('班级信息',               {'fields': ['banji' ,'nickname','banzhuren']}),
        ('作业',               {'fields': ['task',]}),
        ('学生列表',      {'fields': ['studentlist']}),
        ('班主任',           {'fields': ['teacherlist']}),
        ('相册',               {'fields': ['photos']}),
        ('评论',               {'fields': ['comment']}),
    ]
    list_display = ('banji','nickname',)
    filter_horizontal = ('studentlist','teacherlist','task','photos') 
    readonly_fields=('comment','site',)
    list_filter = ('nickname','banji__period','banji__ban','site')
    # print '=====---0======'
    # def save_model(self, request, obj, form, change):
    #     if getattr(obj, 'teacherlist', None) is None:
    #         obj.teacherlist.employee = request.user
    #     obj.last_modified_by = request.user
    #     print '------------'
    #     obj.save()
    def queryset(self, request):
        qs = super(GradeAdmin, self).queryset(request)
        print request.user,'=====uu======'

        if request.user.is_superuser:
            self.readonly_fields=('comment','site',)
            return qs
        else:
            self.readonly_fields=('comment','site','banji','banzhuren')
            return qs.filter(banzhuren__employee__user=request.user)

class ArticleAdmin(admin.ModelAdmin): 
    list_display = ('blog_nav','author','title',)
    readonly_fields=('comment',)
    print '-------000-----'
    def save_model(self, request, obj, form, change):
        if getattr(obj, 'author', None) is None:
            emp=Employee.objects.get(user=request.user)
            obj.author = emp
        obj.save()
    def queryset(self, request):
        qs = super(ArticleAdmin, self).queryset(request)
        # If super-user, show all comments
        if request.user.is_superuser:
            return qs
        return qs.filter(author=request.user)


class AssignmentAdmin(admin.ModelAdmin): 
    list_display = ('teacher','times',)
    list_filter = ('teacher','times','banji')
    # fields=(
    #         ('teacher',),
    #         ('times',),
    #         ('task',),
    #         ('banji'),
    #         ('subject',),
    #         )
    
    # readonly_fields=('site',)
    def save_model(self, request, obj, form, change):
        if request.user.is_superuser:
            obj.save()
        else:
            emp=Employee.objects.filter(user=request.user)
            if len(emp)>0:
                tea=Teacher.objects.get(employee=emp)
                obj.teacher = tea
                obj.save()
            else:
                return HttpResponse('教师不能为空！')
        
    def queryset(self, request):
        qs = super(AssignmentAdmin, self).queryset(request)
        if request.user.is_superuser:
            self.fields=(
            ('teacher',),
            ('times',),
            ('task',),
            ('banji'),
            ('subject',),
            )
            # readonly_fields=('site',)
            return qs

        else:           
            self.fields=(
            ('times',),
            ('task',),
            ('banji'),
            ('subject',),
            )
            self.readonly_fields=('site',)
            tea=Employee.objects.get(user=request.user)
            return qs.filter(teacher=tea)
    

class BlogAdmin(admin.ModelAdmin): 
    list_display = ('title','author','getgrade',)
    list_filter = ('author',)
    readonly_fields=('comment',)
    # fields=(
    #         ('title',),('content'),('comment',),
    #         ('photos',),('types',),
    #         )


    def save_model(self, request, obj, form, change):
        if request.user.is_superuser:
            obj.save()
        else:
            emp=Employee.objects.get(user=request.user)
            print emp.user,'==============empppp'
            obj.author = emp
            obj.save()
        # obj.save()
        
        
    def queryset(self, request):
        qs = super(BlogAdmin, self).queryset(request)
        if request.user.is_superuser:
            self.fields=(
            ('author',),('title',),('content'),('comment',),
            ('photos',),('types',),
            )
            # readonly_fields=('site',)
            return qs

        else:           
            self.fields=(
            ('title',),('content'),('comment',),
            ('photos',),('types',),
            )
            # self.readonly_fields=('site',)
            auth=Employee.objects.get(user=request.user)
            return qs.filter(author=auth)


    


class EmployeeAdmin(admin.ModelAdmin): 
    list_display = ('nickname','sex',)
    # list_filter = (,)

class StudentAdmin(admin.ModelAdmin): 
    list_display = ('employee','getgrade',)
    list_filter = ('isvalid','banji')
    
class TeacherAdmin(admin.ModelAdmin): 
    list_display = ('employee',)
    def queryset(self, request):
        qs = super(TeacherAdmin, self).queryset(request)
        if request.user.is_superuser:
            readonly_fields=('employee',)
            return qs

        else:           
            readonly_fields=('employee',)
            auth=Employee.objects.get(user=request.user)
            return qs.filter(employee=auth)

    # list_filter = ('banji__ban',)
    # def save_model(self, request, obj, form, change):
    #     print '-----start====='
    #     # if getattr(obj, 'subject', None) is None:
    #     #     print obj.employee,'-----------cc---'

    #     print obj.banji.all(),'----cc---'
    #     print '-------ok-----'
    #     obj.save()


class CommentAdmin(admin.ModelAdmin): 
    list_per_page = 50
    list_display = ('content','commenttype','nickname','under','times')
    list_filter = ('commenttype',)
    ordering = ('-times',)
    search_fields = ('content',)
    
    
admin.site.register(Banji,BanjiAdmin)
admin.site.register(Employee,EmployeeAdmin)
# admin.site.register(EmployeeGroup,EmployeeGroupAdmin)
admin.site.register(Article,ArticleAdmin)
admin.site.register(Blog,BlogAdmin)
admin.site.register(Question)
admin.site.register(CommentSort)
admin.site.register(Comment,CommentAdmin)
admin.site.register(Photos)
admin.site.register(PhotosSort,PhotosSortAdmin)
admin.site.register(Grade,GradeAdmin)
admin.site.register(Teacher,TeacherAdmin)
admin.site.register(Student,StudentAdmin)
admin.site.register(Subject)
admin.site.register(Assignment,AssignmentAdmin)

